﻿using System;
using System.Web.Security;
using System.Web.UI.WebControls;
using RazakiSite.UserControls;

namespace RazakiSite.PagesPublic
{
    public partial class Register : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            base.SetDefaultFormControls(RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("CreateUserButton").UniqueID,
                RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("UserName").ClientID);

            //always run
            if (IsPostBack)
            {
                //after postback
                return;
            }
            //first time

            ConfigControls();
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            //empty
        }

        protected void RegisterUser_ActiveStepChanged(object sender, EventArgs e)
        {
            // Have we JUST reached the Complete step?
            if (RegisterUser.ActiveStep.Title == "Complete")
            {
                /*به صورت معمول هر کاربر پس از ثبت نام نقش 
                WizardStep specifyRolesStep = RegisterUser.FindControl("SpecifyRolesStep") as WizardStep;
                RazakiCheckBoxList roleList;

                roleList = specifyRolesStep.FindControl("RoleList") as RazakiCheckBoxList;
                foreach (ListItem li in roleList.Items)
                {
                    if (li.Selected)
                        Roles.AddUserToRole(RegisterUser.UserName, li.Value);
                }*/
                Roles.AddUserToRole(RegisterUser.UserName, ERoles.General.ToString());

                //login====

                FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

                string continueUrl = RegisterUser.ContinueDestinationPageUrl;
                if (String.IsNullOrWhiteSpace(continueUrl))
                {
                    continueUrl = "~/";
                }
                Response.Redirect(continueUrl);
            }
        }

        protected override void ConfigControls()
        {
            /*به صورت معمول هر کاربر پس از ثبت نام نقش
            // Reference the SpecifyRolesStep WizardStep           
            WizardStep SpecifyRolesStep = RegisterUser.FindControl("SpecifyRolesStep") as WizardStep;
            // Reference the RoleList CheckBoxList           
            RazakiCheckBoxList roleList = SpecifyRolesStep.FindControl("RoleList") as RazakiCheckBoxList;
            // Bind the set of roles to RoleList
            AspnetRoleEntity aspnetRoles = new AspnetRoleEntity();

            roleList.DataSource = aspnetRoles.GetAllRolesWithDescription();
            roleList.DataTextField = "Description";
            roleList.DataValueField = "RoleName";
            roleList.DataBind();

            roleList.Enabled = false;
            roleList.Items.Cast<ListItem>().Where(x => x.Value.ToLower() == "general").ToList().ForEach(x => x.Selected = true);
            */

            //old
            //var InfPassword = (UCInfo)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("InfPassword");
            //InfPassword.Text = string.Format(InfPassword.Text, Membership.MinRequiredPasswordLength);

            var InfPassword = (UCInfo)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("InfPassword");
            InfPassword.DataBind();
        }

        protected void RegisterUser_CreateUserError(object sender, CreateUserErrorEventArgs e)
        {
            var uCInfoError = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("UCInfoError") as UCInfo;

            uCInfoError.Hide = false;
            //نتوانستم متن خطا را از کنترل بگیرم
            switch (e.CreateUserError)
            {
                case MembershipCreateStatus.DuplicateEmail:
                    uCInfoError.Text = RegisterUser.DuplicateEmailErrorMessage;
                    break;
                case MembershipCreateStatus.DuplicateUserName:
                    uCInfoError.Text = RegisterUser.DuplicateUserNameErrorMessage;
                    break;
                case MembershipCreateStatus.InvalidEmail:
                    uCInfoError.Text = RegisterUser.InvalidEmailErrorMessage;
                    break;
                case MembershipCreateStatus.InvalidPassword:
                    uCInfoError.Text = RegisterUser.InvalidPasswordErrorMessage;
                    break;
                default:
                    uCInfoError.Text = string.Format("{0} | {1}", RegisterUser.UnknownErrorMessage, e.CreateUserError);
                    break;
            }
        }

    }
}